var eventTrigger = false;
var lastElement  = null;
var offset       = 2;

// Slides (addon)
var index = 0;

$(document).ready(function() {
  $('#zoom').click(function() {
    $('#open').trigger('click'); return false;
  });
  /**
   * Categories (zoom)
   */
  $('.door-element img').mousemove(function() {
    if(!eventTrigger) {
      var position = $(this).position();
      var ob = {
        big   : $('#overlay-' + $(this).parent().parent().attr('id')),
        small : $(this)
      }
      
      ob.big.css({
        top     :  ((position.top + ob.small.height()) - (ob.big.height())  + 'px'),
        left    :  (position.left - ((ob.big.width() - ob.small.width()) / 2) + 'px'),
        display :  'block'
      });
        
      eventTrigger = true;
      lastElement  = ob.big;
      
      updateLastElement();
    }
  });
  
  function updateLastElement() {
    if(lastElement) {
      $(lastElement).mouseleave(function() {
        if(eventTrigger) {
          eventTrigger = false;
          $(this).css({display: 'none'});
        }
      });
    }
  }
  
  /**
   * Slides
   */
  $('#slide').slides({
    play: 3000,
		preload: true,
    hoverPause: false,
		generateNextPrev: false,
    effect: 'slide',
    pagination: true,
    generatePagination: false,
    paginationClass: 'buttons',
    pause: 60000,
    animationComplete: function() {
    	// Change slide names
    	
    }
	});
 
  /**
   * News tabs
   */
  $("#first-tab").click(function() {
    $('#js-location').css('display', 'none');
    $('#js-news').css('display', 'block');
    
    $('#second-tab').css({
      background    :  'url(/web/img/bar.png)',
      textTransform :  'capitalize',
      color         :  '#999'
    });
    
    $(this).css({
      background    :  '#fff',
      textTransform :  'uppercase',
      color         :  '#009fe3'
    });
  });
  
  $('#second-tab').click(function() {
    $('#js-location').css('display', 'block');
    $('#js-news').css('display', 'none');
    
    $('#first-tab').css({
      background    :  'url(/web/img/bar.png)',
      textTransform :  'capitalize',
      color         :  '#999'
    });
    
    $(this).css({
      background    :  '#fff',
      textTransform :  'uppercase',
      color         :  '#009fe3'
    });
  });
  
  /**
   * News button (show more)
   */
		$('.news-button').live('click', function(e) {
			$('#loading-layer').css({
				left: e.pageX - 20,
				top:  e.pageY - 120
			}).fadeTo('slow', 0.8, function() {
				$.get('/index.php/action/newsList/getNextNews?offset=' + offset, function(data) {
					if(data) {
						$('#js-news').html(data); offset += 2;
					} else $('.news-button').css('display', 'none');
					$('#loading-layer').fadeTo('slow', 0, function() {
						$(this).css('display', 'none');
					});
				});
			});
		});
});
